package com.dykj.fireFighting.web.service.impl;/**
 * Created by admin on 2019-9-17.
 */

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.base.effecttimesetting.entity.EffectiveTimeSetting;
import com.dykj.fireFighting.common.core.utils.DateUtil;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.mapper.DevHisMapper;
import com.dykj.fireFighting.web.model.Device;
import com.dykj.fireFighting.web.model.DeviceFault;
import com.dykj.fireFighting.web.model.report.DevHis;
import com.dykj.fireFighting.web.model.report.DevHisWater;
import com.dykj.fireFighting.web.model.report.FaultRecoveryRecord;
import com.dykj.fireFighting.web.model.vo.DevHisVo;
import com.dykj.fireFighting.web.service.DevHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ZhouChunfu
 * @date 2019-9-17
 */
@Service
public class DevHisServiceImpl implements DevHisService {
    @Autowired
    private DevHisMapper devHisMapper;
    @Autowired
    private RedisService redisService;
    @Override
    public int insertHis(DevHis record) {
        String tableName="dev_his_"+DateUtil.getYearWeek();
        return devHisMapper.insertHis(tableName,record);
    }

    @Override
    public DevHis getNewestGz(String relationId) {
        return devHisMapper.getNewestGz(relationId);
    }

    @Override
    public EffectiveTimeSetting getEffectiveTimeSetting(String unitId) {
        return devHisMapper.getEffectiveTime(unitId);
    }

    @Override
    public int insetFaultRecoveryRecord(FaultRecoveryRecord faultRecoveryRecord) {
        return devHisMapper.insertFaultRecoveryRecord(faultRecoveryRecord);
    }

    @Override
    public int insertDevHisWater(DevHisWater devHisWater) {
        String tableName="dev_his_water_"+DateUtil.getYearWeek();
        return devHisMapper.insertDevHisWater(tableName,devHisWater);
    }

    @Override
    public Device queryCreateBy(String relationId) {
        return devHisMapper.queryCreateBy(relationId);
    }

    @Override
    public int updateTopRui(Device device) {
        return devHisMapper.updateTopRui(device);
    }

    @Override
    public String getWaterDevStatus(String relationId) {
        return devHisMapper.getWaterDevStatus(relationId);
    }

    @Override
    public List<Device> getDevices(Map<String, String> params) {
        return devHisMapper.getDevices(params);
    }

    @Override
    public List<DevHisVo> getDevHiss(String[] devIds, String ztfssj) {
        String tableName="dev_his_"+DateUtil.getYearWeek();
        return devHisMapper.getDevHisByIds(tableName,devIds,ztfssj);
    }

    @Override
    public int insertOrUpdateFaultDevice(DeviceFault deviceFault) {
        DeviceFault deviceFault1 = devHisMapper.queryByRelationId(deviceFault.getRelationId());
        int result;
        if(deviceFault1 == null ){
          result=  devHisMapper.insertFaultDevice(deviceFault);
        }else{
            result=devHisMapper.updateFaultDevice(deviceFault);
        }
        redisService.set(deviceFault.getRelationId()+"-fault", JSON.toJSONString(deviceFault));
        return result;
    }

    @Override
    public DeviceFault queryById(String relationId) {
        DeviceFault deviceFault=(DeviceFault) redisService.get(relationId + "-fault");
        if(deviceFault==null){
            deviceFault=devHisMapper.queryByRelationId(relationId);
        }
        return deviceFault;
    }

    @Override
    public int deleteFaultDevice(String relationId) {
        redisService.del(relationId+"-fault");
        return devHisMapper.deleteFaultDevice(relationId);
    }

}
